﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Commercial;

public partial class Administrator_Brand_Update : System.Web.UI.Page
{
    int isVip = 0;
    int isCheck = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Brand brand = new Brand();
            int id = Convert.ToInt32(Request.QueryString["id"]);
            var query = brand.DetailBrand(id);
            TextBox_BrandName.Text = query.name;
            TextBox_BrandDescription.Text = query.description;
            HiddenField_Logo.Value = "~/uploads/" + query.logo;
            Image_Logo.ImageUrl = "~/uploads/" + query.logo;
            TextBox_BrandUrl.Text = query.url;

            Jobs jobs = new Jobs();
            DropDownList_Job.DataSource = jobs.ListJob();
            DropDownList_Job.DataValueField = "id";
            DropDownList_Job.DataTextField = "job_name";
            DropDownList_Job.SelectedValue = query.category_job;
            DropDownList_Job.DataBind();

            TextBox_BrandCustomer.Text = query.customer_brand;
            TextBox_BrandCustomerAddress.Text = query.address;
            TextBox_Phone.Text = query.phone;
            TextBox_MobilePhone.Text = query.mobile;
            TextBox_Email.Text = query.email;
            TextBox_Fax.Text = query.fax;

            //TextBox_City.Text = query.city;
            Cities cities = new Cities();
            DropDownList_City.DataSource = cities.ListCity();
            DropDownList_City.DataTextField = "city_name";
            DropDownList_City.DataValueField = "id";
            DropDownList_City.SelectedValue = query.id_city.Value.ToString();
            DropDownList_City.DataBind();

            Districts districts = new Districts();
            DropDownList_District.DataSource = districts.ListDistrict(query.id_city.Value);
            DropDownList_District.DataTextField = "district_name";
            DropDownList_District.DataValueField = "id";
            DropDownList_District.DataBind();

            TextBox_TaxCode.Text = query.tax_code;
            this.isVip = query.isVip.Value;
            this.isCheck = query.isCheck.Value;
        }
    }
    protected void DropDownList_City_SelectedIndexChanged(object sender, EventArgs e)
    {
        int id_city = Convert.ToInt32(DropDownList_City.SelectedValue);
        Districts districts = new Districts();
        DropDownList_District.DataSource = districts.ListDistrict(id_city);
        DropDownList_District.DataTextField = "district_name";
        DropDownList_District.DataValueField = "id";
        DropDownList_District.DataBind();
    }
    protected void Button_UpdateBrand_Click(object sender, EventArgs e)
    {
        string[] splitFileName = HiddenField_Logo.Value.Split('/');
        string fileName = splitFileName[splitFileName.Length - 1];

        int id_district = 0;
        int id_city = Convert.ToInt32(DropDownList_City.SelectedValue);
        if (DropDownList_District.SelectedValue != "")
        {
            id_district = Convert.ToInt32(DropDownList_District.SelectedValue);
        }

        Brand brand = new Brand();
        int id = Convert.ToInt32(Request.QueryString["id"]);
        brand.UpdateBrand(id, TextBox_BrandName.Text.Trim(), TextBox_BrandDescription.Text.Trim(), fileName, TextBox_BrandUrl.Text.Trim(), DropDownList_Job.SelectedValue, TextBox_BrandCustomer.Text.Trim(), TextBox_BrandCustomerAddress.Text.Trim(), TextBox_Phone.Text.Trim(), TextBox_MobilePhone.Text.Trim(), TextBox_Email.Text.Trim(), TextBox_Fax.Text.Trim(), TextBox_TaxCode.Text.Trim(), this.isVip, this.isCheck, id_city, id_district);
        Response.Redirect("~/Administrator/ManagerBrand.aspx");
    }
}